package com.javacafe.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import com.javacafe.MainMenu;

import android.content.Intent;
import android.util.Log;

public class Server{

	String protocol;

	String id;
	String pass;
	String phone;
	String email;

	PrintWriter out;
	BufferedReader in;

	Intent intent;
	public String bCheck="";
	
	public Server(String protocol, String id, String pass) {

		this.protocol = protocol;
		this.id = id;
		this.pass = pass;
		sendMessage();
	}

	public Server(String protocol, String id, String pass, String phone,
			String email) {

		this.protocol = protocol;
		this.id = id;
		this.pass = pass;
		this.phone = phone;
		this.email = email;
		sendMessage();
	}

	public void sendMessage() {
		try {

			Socket socket = new Socket("121.162.249.114", 9999);

			String message;
			try {
				out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				Log.i("proto", protocol);
				if (protocol.equals("100")) {
					message = id + "|" + pass;
					out.println("100|" + message);
					this.bCheck = in.readLine();
					Log.e("bCheck", bCheck);
					
						
				} else if (protocol.equals("200")) {
					message = id + "|" + pass + "|" + phone + "|" + email;
					out.println("200|" + message);

					bCheck = in.readLine();
					Log.e("bCheck", bCheck);

				}

			} catch (Exception e) {
				Log.e("TCP", "S: Error", e);
			} finally {
				socket.close();
			}
		} catch (Exception e) {
			Log.e("TCP", "C: Error", e);
		}
	}

}